package edu.java.homework.hw03.ipj06;

import java.util.Scanner;

/**
 * This class realize input and output of 2 dimensional array, and also editing
 * of the elements of the array
 * 
 * @author Tsvetan Vasilev
 * 
 */
public class Exer01 {

    /**
     * tests faithfullness of the task
     * 
     * @param args
     */
    public static void main(String[] args) {
	double[][] array;
	int rowCount = 0;
	int columnCount = 0;
	Scanner input = new Scanner(System.in);
	System.out.println("Enter the 2 dimentions of the array.");
	rowCount = input.nextInt();
	System.out.printf("Rows:\t\t%d\n", rowCount);
	columnCount = input.nextInt();
	System.out.printf("Columns:\t%d\n", columnCount);

	// input parameter verification done perfectly! :)
	// TODO: now improve it - create method, which reads only row/column and
	// before returning the result, the method to verify it. If it is wrong,
	// for example negative number, to ask the user to enter correct number 
	// again
	if (rowCount < 2 || columnCount < 2) {
	    throw new IllegalArgumentException(
		    "Incorrect input of the dimentions of the array ");
	}
	array = new double[rowCount][columnCount];
	inputArray(array);
	System.out.println("The array before editing: ");
	outputArray(array);
	System.out.println("\nThe array after editng: ");
	editArray(array);
	outputArray(array);

	input.close();
    }

    /**
     * create and initialize a 2D array
     */
    public static void inputArray(double[][] array) {
	// TODO: input parameter verification

	Scanner input = new Scanner(System.in);

	for (int i = 0; i < array.length; i++) {
	    for (int j = 0; j < array[0].length; j++) {
		System.out.printf("arr[%d][%d]= ", i, j);

		// TODO: input parameter verification
		array[i][j] = input.nextDouble();

		System.out.println();
	    }
	}
	input.close();
    }

    /**
     * print a 2D array
     */
    public static void outputArray(double[][] array) {
	// TODO: input parameter verification

	// 1st easy way of printing --> using API method Arrays.deepToString
	// System.out.println(Arrays.deepToString(array));

	// 2nd way --> my realisation
	for (int i = 0; i < array.length; i++) {
	    for (int j = 0; j < array[0].length; j++) {
		System.out.printf("%.2f\t", array[i][j]);
	    }
	    System.out.println();
	}
    }

    /**
     * increment with 1 every element of the array
     * 
     * @param array
     */
    public static void editArray(double[][] array) {
	// TODO: input parameter verification
	for (int i = 0; i < array.length; i++) {
	    for (int j = 0; j < array[0].length; j++) {
		array[i][j] += 1;
	    }
	}
    }

}
